###### Libraries ---------------------------------------------------------------
library(tidyverse)
library(readr)

##### Load data ----------------------------------------------------------------
# prolific demographics
demographics <- read_csv("data/prolific_export_67238a48ed7463b220b40584.csv") |> 
  dplyr::select("Participant id", "Time taken", "Age", "Sex", "Ethnicity simplified",
                "Country of birth", "Country of residence", "Nationality", "Language", 
                "Student status", "Employment status") |> 
  rename(duration = "Time taken",
         ethnicity = "Ethnicity simplified",
         country_birth = "Country of birth",
         country_residence = "Country of residence",
         student = "Student status",
         employment = "Employment status")

# survey data
difdef <- read_csv("data/difdef_raw.csv") |> 
  slice(-1, -2) |> 
  
  # Convert all to integers
  mutate(across(c("leri", "dem_satis", "dem_sup", "pol_interest", "pol_news", 
                  "pol_trust_inparty", "pol_trust_outparty", "pol_trust_judges", "pol_trust_parl",
                  "pol_trust_govt", "pol_trust_journ", "pol_trust_part", "pol_trust_mayor",
                  "pol_trust_acad", "pol_trust_ngo", "pol_trust_mili",
                  "eval_pre", "eval_post",
                  "perception_neut", "perception_recog", "perception_leri", "perception_comt"), as.integer)) |> 
  
  # Attention and manipulation
  mutate(attention = case_when(attention == "4" ~ "attentive",
                               attention %in% c("1", "2", "3", "5") ~ "inattentive",
                               TRUE ~ NA),
         manipulation = case_when(manipulation == "1" ~ "manipulated",
                                  manipulation == "2" ~ "unmanipulated",
                                  TRUE ~ NA)) |> 
  
  # In-party
  mutate(inparty = case_when(inparty == "1" ~ "labour",
                             inparty == "2" ~ "conservative",
                             inparty == "3" ~ "libdem",
                             inparty == "4" ~ "snp",
                             inparty == "5" ~ "sinnfein",
                             inparty == "6" ~ "reformuk",
                             inparty == "7" ~ "dup",
                             inparty == "8" ~ "green",
                             inparty == "9" ~ "plaidcymru",
                             inparty == "10" ~ "socialdems",
                             inparty == "11" ~ "allianceparty",
                             inparty == "12" ~ "uup",
                             inparty == "13" ~ "tuv")) |> 
  
  # Treatment
  mutate(actor = case_when(ACTOR2 == "the judge" ~ "judge",
                           ACTOR2 == "the civil servant" ~ "civil servant",
                           ACTOR2 == "the military official" ~ "military official",
                           ACTOR2 == "the opposing parliamentarian" ~ "out-party",
                           ACTOR2 == "your mayor" ~ "mayor",
                           ACTOR2 == "the journalist" ~ "journalist",
                           ACTOR2 == "the international human rights watch dog" ~ "NGO",
                           ACTOR2 == "the professor" ~ "academic",
                           TRUE ~ "in-party"),
         type = case_when(ACTOR2 == "the judge" ~ "institutional",
                          ACTOR2 == "the civil servant" ~ "institutional",
                          ACTOR2 == "the military official" ~ "institutional",
                          ACTOR2 == "the opposing parliamentarian" ~ "political",
                          ACTOR2 == "your mayor" ~ "political",
                          ACTOR2 == "the journalist" ~ "social",
                          ACTOR2 == "the international human rights watch dog" ~ "social",
                          ACTOR2 == "the professor" ~ "social",
                          TRUE ~ "political")) |> 
  
  # Main outcome
  mutate(outcome = eval_pre - eval_post) |> 
  
  # LeRi-bias, reverse neutrality, mayor-identification, out-party-identification, political trust mean
  mutate(leri_bias = abs(perception_leri - 5) * 2,
         leri_distance = abs(leri - perception_leri),
         perception_neut = 10 - perception_neut, # reverse so that higher scores mean more partial
         mayor = case_when(mayor == "1" ~ 1,
                           mayor == "2" | mayor == "3" ~ 0,
                           actor == "mayor" & is.na(mayor) ~ 0,
                           TRUE ~ 0),
         opposite = case_when(opposite == "1" ~ "no specific",
                              opposite == "2" ~ "labour",
                              opposite == "3" ~ "conservative",
                              opposite == "4" ~ "libdem",
                              opposite == "5" ~ "snp",
                              opposite == "6" ~ "sinnfein",
                              opposite == "7" ~ "reformuk",
                              opposite == "8" ~ "dup",
                              opposite == "9" ~ "green",
                              opposite == "10" ~ "plaidcymru",
                              opposite == "11" ~ "socialdems",
                              opposite == "12" ~ "allianceparty",
                              opposite == "13" ~ "uup",
                              opposite == "14" ~ "tuv",
                              actor != "out-party" ~ "0"),
         pol_trust_mean = rowMeans(across(starts_with("pol_trust")), na.rm = TRUE)) |> 
  dplyr::select(-mayor_2_TEXT) |> 
  
  # Do respondents expect elite democratic defence?
  mutate(expected = case_when(!is.na(expected) ~ "yes",
                              is.na(expected) ~ "no")) |> 
    
  # Merge with demographics from prolific
  left_join(demographics, by = c("PROLIFIC_PID" = "Participant id")) |> 
  mutate(Age = case_when(Age >= 18 & Age <= 24 ~ "18-24",
                         Age >= 25 & Age <= 34 ~ "25-34",
                         Age >= 35 & Age <= 44 ~ "35-44",
                         Age >= 45 & Age <= 54 ~ "45-54",
                         Age >= 55 ~ "55+",
                         TRUE ~ NA)) |> 
  
  # Remove respondents who don't consent and who did not finish the survey
  filter(consent == 1) |> 
  filter(Finished == 1) |> 
  filter(ethnicity != "CONSENT_REVOKED") |> 
  
  # Remove meta-information
  dplyr::select(-StartDate, -EndDate, -Status, -IPAddress, -Progress, -Finished,
                -RecipientLastName, -RecipientFirstName, -RecipientEmail, -ExternalReference,
                -LocationLatitude, -LocationLongitude, -DistributionChannel, -UserLanguage,
                -consent, -Q25)

##### Open answers -------------------------------------------------------------
open <- difdef |> 
  dplyr::select(actor, type, eval_pre, eval_post, outcome, ResponseId, explain_as, explain_more, explain_less, duty) |> 
  filter(!if_all(c(explain_as, explain_more, explain_less, duty), is.na))

##### Expected democratic defenders --------------------------------------------
expected <- read_csv("data/difdef_raw.csv") |> 
  dplyr::select(ResponseId, ACTOR2, expected) |> 
  slice(-1, -2) |> 
  mutate(actor = case_when(ACTOR2 == "the judge" ~ "judge",
                           ACTOR2 == "the civil servant" ~ "civil servant",
                           ACTOR2 == "the military official" ~ "military official",
                           ACTOR2 == "the opposing parliamentarian" ~ "out-party",
                           ACTOR2 == "your mayor" ~ "mayor",
                           ACTOR2 == "the journalist" ~ "journalist",
                           ACTOR2 == "the international human rights watch dog" ~ "NGO",
                           ACTOR2 == "the professor" ~ "academic",
                           TRUE ~ "in-party")) |> 
  dplyr::select(!ACTOR2) |> 
  separate_longer_delim(expected, delim = ",") |> 
  mutate(expected = case_when(expected == "1" ~ "in-party",
                              expected == "2" ~ "out-party",
                              expected == "3" ~ "military official",
                              expected == "4" ~ "judge",
                              expected == "5" ~ "civil servant",
                              expected == "6" ~ "mayor",
                              expected == "7" ~ "journalist",
                              expected == "8" ~ "NGO",
                              expected == "9" ~ "academic",
                              expected == "10" ~ "other",
                              TRUE ~ NA))

expected_text <- difdef |> 
  dplyr::select(ResponseId, expected_10_TEXT) |> 
  filter(!is.na(expected_10_TEXT))


  
##### Export data --------------------------------------------------------------
write.csv(difdef,
          file = "data/difdef.csv",
          row.names = FALSE)

write.csv(expected,
          file = "data/expected.csv",
          row.names = FALSE)

write.csv(expected_text,
          file = "data/expected_text.csv",
          row.names = FALSE)

write.csv(open,
          file = "data/open.csv",
          row.names = FALSE)

# /./ End of Code /../ #
